package com.vornitologist.ui;

import java.util.List;
import java.util.ResourceBundle;

import org.activiti.engine.repository.ProcessDefinition;

import com.guruyu.hawaii.interfaces.HawaiiFactory;
import com.vaadin.addon.touchkit.ui.NavigationView;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Table;

public class MyProcess extends NavigationView implements ClickListener {

    private Button exit = new Button(null, this);

    private ResourceBundle tr;

    private Table table = new Table();

    @Override
    public void attach() {
        super.attach();
        if (tr == null) {
            buildView();
        }
    }

    private void buildView() {
        //tr = Translations.get(getLocale());
        setCaption("Procesos");
        table.setSizeFull();
        populateTable();
        table.setSelectable(true);
        setContent(table);

        exit.setIcon(new ThemeResource("linegraphics/home.png"));
        setRightComponent(exit);

        table.addListener(new Table.ValueChangeListener() {
			public void valueChange(ValueChangeEvent event) {
				System.out.println("B"+table.getValue());
				System.out.println("C"+event.getProperty().getValue());
				
				if (table.getValue()!=null){
            		getNavigationManager().navigateTo(new StartProcessView(table.getValue()));
            	}
			}
        });
        
        table.addListener(new ItemClickEvent.ItemClickListener() {
			public void itemClick(ItemClickEvent event) {
				System.out.println("A "+event.getItem());
				}
			});

    } 

    private void populateTable() {
    	
//    	List<ProcessDefinition> list = HawaiiFactory.getInstance().getIEngineController().getAllProcessDefinition();
    	String userId = (String) getApplication().getUser();
    	List<ProcessDefinition> list = HawaiiFactory.getInstance().getIEngineController().getUserProcessDefinition(userId);
    	BeanItemContainer<ProcessDefinition> dataSource = new BeanItemContainer<ProcessDefinition>(
    			ProcessDefinition.class, list);
    	table.setContainerDataSource(dataSource);
    	table.setVisibleColumns(new String[]{"id","name"});
    }

    public void buttonClick(ClickEvent event) {
        if (exit == event.getButton()) {
        	getWindow().setContent(new LoginView(getWindow()));
        } else {
        }
    }



}
